<template>
  <transition>
    <div class="app-shopcart">
      <section class="app-main">

        <div class="product-content" v-for="item in shopCartList">
        <div class="item-checkbox">
               <span class="radio_btn">
                  <input class="check-position" type="checkbox" v-model="item.isCheck">
                  <span></span>
              </span>
        </div>
          <div class="product-img">
            <img :src="noneImg"/>
          </div>
          <ul class="product-details">
            <li class="item item-title">
              {{item.commAttribName}}
              <span class="item-factory">{{item.factory}}</span>
            </li>
            <li class="item item-price">
              ￥<span class="price">{{item.commattribPrice}}</span>
              <div class="select-count">
                <span class="item item-reduce js-item-reduce" @click="handleModify(item,'reduce')">-</span>
                <input class="item item-input js-item-input" type="number" v-model="item.count"/>
                <span class="item item-add js-item-add" @click="handleModify(item,'add')">+</span>
              </div>
            </li>
          </ul>
        </div>
        <div class="button-content-bottom">
        <span class="content-left">
          <label>
              <span class="radio_btn">
                   <input class="check-position" type="checkbox" v-model="checkAll">
                   <span></span>
              </span>
            全选</label>
          <span class="price-num">合计：￥{{priceNum}}</span>
        </span>
          <mt-button type="primary" @click.native="handleToPay">去结算({{checkedAllCount}})</mt-button>
        </div>
      </section>
    </div>
  </transition>
</template>

<script>
  import {Header, Cell, Toast, MessageBox} from 'mint-ui';
  //  Vue.component(Header.name, Header);
  export default {
    components: {
      Header,
      Cell,
    },
    data () {
      return {
        picList: [],
        noneImg: './static/img/img-none.jpg',
        shopCartList: [{
          commattribId:'6bd4b5bba88b4ab6be140afedfe128d9',
          commAttribName: '奥迪TT Coupe(15-X)硬顶跑车前挡/灰带/雨感支架/包边条',
          commattribPrice: 840,
          factory: '同质件',
          count: 1,
          isCheck: true,
          serviceAttr:[
          {
            name:'工时费',
            price:50
          }
          ],
        }, {
          commattribId:'d11938a7fbf24544b584c86318424a35',
          commAttribName: '奥迪TT Coupe(15-X)硬顶跑车前挡/雨感支架/摄像支架/加热膜/包边',
          commattribPrice: 1080,
          factory: '同质件',
          count: 1,
          isCheck: false,
          serviceAttr:[
            {
              name:'工时费',
              price:150
            }
          ],
        }]
      }
    },
    computed:{
      checkAll:{
        get(){
          return this.checkedCount==this.shopCartList.length;
        },
        set: function(value) {
          console.log(value);
          this.shopCartList = this.shopCartList.map(function(item){item.isCheck=value;return item;});
        }
      },
      checkedCount:{
        get: function() {
          return this.shopCartList.filter(function(item){
            return item.isCheck}
          ).length;
        }
      },
      checkedAllCount() {
        var count=0;
        const checkedList=this.shopCartList.filter(function(item){
          return item.isCheck}
        );
        checkedList.forEach(function(item){
          count+=item.count;
        });
        return count;
      },
      priceNum:{
        get:function(){
          const checkedList=this.shopCartList.filter(function(item){
            return item.isCheck}
          );
          var price=0;
          checkedList.forEach(function(item){
            price+=(item.commattribPrice)*(item.count);
          });
          return price;
        }
      }
    },
    mounted(){
    },
    methods: {
      handleModify(item,type){

        if(type=='reduce'){
          if(item.count<=1){
            Toast({
              message: '购买数量不能小于1',
              position: 'middle',
              duration:1200
            });
            return false;
          }else{
            item.count--;
          }

        }else{
          item.count++;
        }

      },
      handleToPay(){

        var orderList=[];
        for(var i=0;i<this.shopCartList.length;i++){
          if(this.shopCartList[i].isCheck==true){
            orderList.push(this.shopCartList[i])
          }
        }

        sessionStorage.removeItem('orderList');
        sessionStorage.setItem('orderList',JSON.stringify(orderList))
        this.$router.push({path:'/store-inquiry-order'})
      },
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style>

  .app-shopcart .product-content {
    background: #fff;
    margin: 0 0 12px;
    padding: 12px;
    font-size: 14px;
  }

  .app-shopcart .product-content .item-checkbox {
    display: table-cell;
    vertical-align: middle;
    width:8%;
  }

  .app-shopcart .product-content .product-img {
    display: table-cell;
    vertical-align: top;
    padding-right: 12px;
    width: 32%;
  }

  .app-shopcart .product-content .product-img img {
    width: 100px;
    height: 100px;
  }

  .app-shopcart .product-content .product-details {
    position: relative;
    display: table-cell;
    vertical-align: top;
    width: 60%;
  }

  .app-shopcart .product-content .item {
    margin: 0 0 4px;
  }

  .app-shopcart .product-content .item-title {
    font-size: 15px;
    color: #000;
    margin: 0 0 6px;
    line-height: 22px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;

  }

  .app-shopcart .product-content .item-price {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12px 0 0;
    color: red;
    font-size: 16px;
  }

  .app-shopcart .product-content .item-img {
    margin: 0 0 10px;
  }

  .app-shopcart .select-count {
    float: right;
    display: inline-block;
    border: 1px solid #eee;
    border-radius: 5px;
  }

  .app-shopcart .select-count .item {
    display: inline-block;
    vertical-align: middle;
    width: 30px;
    height: 28px;
    font-size: 16px;
    text-align: center;
    font-weight: bold;
    line-height: 22px;
    margin: 0;
  }

  .app-shopcart .select-count .item-reduce {
    font-size: 20px;
    color: #0080e5;
    border-right: 1px solid #eee;
  }

  .app-shopcart .select-count .item-add {
    font-size: 20px;
    color: #0080e5;
    border-left: 1px solid #eee;
  }

  .app-shopcart .select-count .item-input {
    width: 32px;
    outline: none;
    border: none;
    font-weight: normal;
  }

  .app-shopcart .item-factory {
    border: 1px solid red;
    margin-left: 8px;
    padding: 0 4px;
    border-radius: 3px;
    font-size: 12px;
    color: red;
    letter-spacing: 1px;
  }

  .app-shopcart .button-content-bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: #fff;
    display: flex;
    height: 54px;
    padding: 0 16px;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0px -6px 4px 0px rgba(0, 0, 0, 0.03);
  }

  .app-shopcart .button-content-bottom .mint-button {
    justify-content: center;
    font-size: 15px;
    box-shadow: none;
    height: 36px;
    margin-left: 12px;

  }

  .app-shopcart .button-content-bottom .content-left {
    font-size: 14px;
    color: #666;
  }

  .app-shopcart .button-content-bottom .price-num {
    padding-left: 6px;
    font-size: 16px;
    color: red;
  }

  .app-shopcart .radio_btn {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    width: 20px;
    height: 20px;
  }

  .app-shopcart .radio_btn input {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
    opacity: 0;
    margin: 0;
  }

  .app-shopcart .radio_btn span {
    background: #fff;
    display: block;
    width: 20px;
    height: 20px;
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    border: 1px solid #ddd;
    border-radius: 50%;
  }

  .app-shopcart .radio_btn input[type="checkbox"] + span {
    background: #fff;

  }

  .app-shopcart .radio_btn input[type="checkbox"]:checked + span {
    border: 1px solid #26a2ff;
    background: #26a2ff;
  }

  .app-shopcart .radio_btn span:after {
    border: 2px solid transparent;
    border-left: 0;
    border-top: 0;
    content: " ";
    top: 3px;
    left: 6px;
    position: absolute;
    width: 4px;
    height: 8px;
    -webkit-transform: rotate(45deg) scale(0);
    transform: rotate(45deg) scale(0);
    -webkit-transition: -webkit-transform .2s;
    transition: -webkit-transform .2s;
    transition: transform .2s;
    transition: transform .2s, -webkit-transform .2s;
  }

  .app-shopcart .radio_btn input[type="checkbox"]:checked + span:after {
    border-color: #fff;
    -webkit-transform: rotate(45deg) scale(1);
    transform: rotate(45deg) scale(1);
  }

  .app-shopcart .button-content-bottom .radio_btn{
    position: relative;
    top: -2px;
  }
</style>
